package com.strawcom.charmring.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import com.android.volley.Request.Method;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bean.http.BaseResponse;
import com.strawcom.charmring.bean.http.PostUserComplaintRequest;
import com.strawcom.charmring.util.HttpUtil;
import com.strawcom.charmring.util.HttpUtil.ResponeListener;
import com.strawcom.charmring.view.CustomProgressDialog;
import com.strawcom.charmring.view.TitleView;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;

public class AdviceActivity extends BaseActivity implements OnClickListener {

	private TitleView titleView;
	private Button btnAdvice;
	private View layoutAdvice1, layoutAdvice2, layoutAdvice3, layoutAdvice4,
			layoutAdvice5, layoutAdvice6, layoutAdvice7;
	private int[] choicelist = new int[7];
	private ImageView img1, img2, img3, img4, img5, img6, img7;
	private EditText editAdvice;

	private View layoutOther;
	private CustomProgressDialog dialog;
	private ListView listview;
	private ScrollView scrollview;
	private TextView tv_space;
	ArrayList<String> list = new ArrayList<String>();

	private String[] adviceTextList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_advice);
		super.onCreate(savedInstanceState);
		initView();
		dialog = CustomProgressDialog.createDialog(this, false).setMessage(
				R.string.wating_advice);
	}

	private void initView() {
		
		/*tv_space = (TextView) findViewById(R.id.tv_space);
		//tv_space.setVisibility(View.GONE);
		tv_space.setHeight(0);*/

		editAdvice = (EditText) findViewById(R.id.edit_advice);

		titleView = (TitleView) findViewById(R.id.title_view);
		//titleView.setTitleText(R.string.title_advice);
		btnAdvice = (Button) findViewById(R.id.btn_advice);
		btnAdvice.setOnClickListener(this);

		/*layoutAdvice1 = findViewById(R.id.layout_advice_1);
		layoutAdvice2 = findViewById(R.id.layout_advice_2);
		layoutAdvice3 = findViewById(R.id.layout_advice_3);
		layoutAdvice4 = findViewById(R.id.layout_advice_4);
		layoutAdvice5 = findViewById(R.id.layout_advice_5);
		layoutAdvice6 = findViewById(R.id.layout_advice_6);*/
		layoutAdvice7 = findViewById(R.id.layout_advice_7);

		listview = (ListView) findViewById(R.id.advice_list);
		adviceTextList = new String[]{getResources().getString(R.string.advice_1),
									getResources().getString(R.string.advice_2), 
									getResources().getString(R.string.advice_3), 
									getResources().getString(R.string.advice_4), 
									getResources().getString(R.string.advice_5), 
									getResources().getString(R.string.advice_6),
		" "};
		list.add(getResources().getString(R.string.advice_1));
		list.add(getResources().getString(R.string.advice_2));
		list.add(getResources().getString(R.string.advice_3));
		list.add(getResources().getString(R.string.advice_4));
		list.add(getResources().getString(R.string.advice_5));
		list.add(getResources().getString(R.string.advice_6));
		AdviceListAdapter adapter = new AdviceListAdapter(AdviceActivity.this, 
				R.layout.item_advice_list, list);
		System.out.println("adviceTextList's length is " + adviceTextList.length);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				choicelist[position] = (choicelist[position]==1?0:1);
				ImageView iv = ((AdviceListAdapter.ViewCache)(view.getTag())).iv;
				iv.setImageResource(choicelist[position]==1?R.drawable.chk_normal:R.drawable.chk_select);
			}
		});
		scrollview = (ScrollView) findViewById(R.id.scrollview);
		listview.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					scrollview.requestDisallowInterceptTouchEvent(false);
				} else {
					scrollview.requestDisallowInterceptTouchEvent(true);
				}
				return false;
			}
		});
		Utility.setListViewHeightBasedOnChildren(listview);
		
		layoutOther = findViewById(R.id.layout_other);
		layoutOther.setVisibility(View.GONE);
		

		/*img1 = (ImageView) findViewById(R.id.img_chk_1);
		img2 = (ImageView) findViewById(R.id.img_chk_2);
		img3 = (ImageView) findViewById(R.id.img_chk_3);
		img4 = (ImageView) findViewById(R.id.img_chk_4);
		img5 = (ImageView) findViewById(R.id.img_chk_5);
		img6 = (ImageView) findViewById(R.id.img_chk_6);*/
		img7 = (ImageView) findViewById(R.id.img_chk_7);

		/*layoutAdvice1.setOnClickListener(this);
		layoutAdvice1.setTag(false);
		layoutAdvice2.setOnClickListener(this);
		layoutAdvice2.setTag(false);
		layoutAdvice3.setOnClickListener(this);
		layoutAdvice3.setTag(false);
		layoutAdvice4.setOnClickListener(this);
		layoutAdvice4.setTag(false);
		layoutAdvice5.setOnClickListener(this);
		layoutAdvice5.setTag(false);
		layoutAdvice6.setOnClickListener(this);
		layoutAdvice6.setTag(false);*/
		layoutAdvice7.setOnClickListener(this);
		layoutAdvice7.setTag(false);

		for (int i = 0; i < choicelist.length; i++) {
			choicelist[i] = 1; // 变成false; 服务器 要求0,1   ... 0 true ;1 false
		}
	}

	private void advice() {
		PostUserComplaintRequest complaintRequest = new PostUserComplaintRequest();
		complaintRequest.msgid = C.request_msgid_post_user_complaint;
		
		StringBuffer sb = new StringBuffer();
		String s7 = "";
		if(choicelist[6] == 0) {
			if (!TextUtils.isEmpty(editAdvice.getText().toString())) {
				adviceTextList[6] = editAdvice.getText().toString();
				complaintRequest.msg = adviceTextList[6];
				//sb.append(editAdvice.getText().toString());
			} else {
				showToast(R.string.no_advice);
				return;
			}
		}
		
		
		
		//上传的内容 edit by zls
		int size = 0;
		//获取上传列表的size
		for (int i = 0; i < choicelist.length; i++) {
			if(choicelist[i] == 0){
				size++;
			}
		}System.out.println("size = " + size);

//		complaintRequest.msg = new String[size];

		int[] submitList = new int[size];
		int j = 0;
		//获取上传列表的内容的在choicelist中的index
		for (int i = 0; i < choicelist.length; i++) {
			if(choicelist[i] == 0){
				submitList[j] = i + 1;
//				complaintRequest.msg[j] = adviceTextList[i];
//				System.out.println("msg = " + complaintRequest.msg);
				j++;
			}
		}
		System.out.println("submitList = " + submitList);
		
		dialog.show();
		complaintRequest.choiceResult = submitList;
		for (int i = 0; i < submitList.length; i++) {
			System.out.println("submitList[" + i + "] = " + submitList[i]);
		}
		
		HttpUtil.requestJson(Method.POST, complaintRequest,
			new ResponeListener() {
				@Override
				public void onResponse(BaseResponse response,
						String resultStr) {
					dialog.dismiss();
					showToast(response.retcode == 0 ? R.string.submit_success : R.string.request_error);
				}
				@Override
				public void onErrorResponse(Exception error) {
					dialog.dismiss();
					showToast(SystemTool.checkNet(AdviceActivity.this) ? R.string.request_error : R.string.net_error);
				}
			});
	}

	@Override
	public void onClick(View view) {
		boolean check = false;
		if (null != view.getTag()) {
			check = Boolean.valueOf(view.getTag().toString());
			check = !check;
			view.setTag(check);
		}
		
		switch (view.getId()) {
		case R.id.btn_advice:
			advice();
			break;
		/*case R.id.layout_advice_1:
			img1.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[0] = check ? 0 : 1;
			break;
		case R.id.layout_advice_2:
			img2.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[1] = check ? 0 : 1;
			break;
		case R.id.layout_advice_3:
			img3.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[2]  = check ? 0 : 1;
			break;
		case R.id.layout_advice_4:
			img4.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[3] = check ? 0 : 1;
			break;
		case R.id.layout_advice_5:
			img5.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[4] = check ? 0 : 1;
			break;
		case R.id.layout_advice_6:
			img6.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[5]  = check ? 0 : 1;
			break;*/
		case R.id.layout_advice_7:
			img7.setImageResource(check ? R.drawable.chk_select
					: R.drawable.chk_normal);
			choicelist[6]  = check ? 0 : 1;
			layoutOther.setVisibility(check ? View.VISIBLE : View.GONE);
			/*tv_space.setVisibility(choicelist[6]==1?View.GONE:View.VISIBLE);*/
			//tv_space.setHeight(choicelist[6]==1?0:30);
			break;
		}
	}
}
